package client;
import gui.Survey_GUI;
import client.MainScreen;
import java.io.Serializable;
//import client.ObjHandle;  sendToServer(obj)
/**
 * survey controller
 * control survey handling
 * @author G6
 * @version June 2011
 */
public class Survey implements Serializable
{
	/** sid - survey id */
	private int sid;
	/** distribute - distributed survey */
	int distribute=-1;
	/** analyze - analyzed survey */
	private String analyze="-1";
	/** q[] - survey questions */
	private String []q = null;
	
	
	 /**
     * Survey contractor
     * @param s - the survey sid to set
     */
	public Survey(int s)
	{
		sid = s;
		distribute=0;
		q = new String [6];
	}
	
	 /**
     * Survey contractor
     */
	public Survey()
	{
		sid=0;
		distribute=0;
		q = new String [6];
	}
	
	/**
     * @param qi - the q (questions) to set
     */
	public void setQi(String[] qi)
	{
		for (int i=0; i< qi.length ;i++)
			q[i]=qi[i];
	}
	
	/**
     * @param s - the sid to set
     */
	public void setSid(int s)
	{sid=s;}
	
	/**
     * @param a - the analyze to set
     */
	public void setAnalyse(String a)
	{this.analyze=a;}
	
	/**
     * @param s - the distribute value to set
     */
	public void setDistribute(int s)
	{distribute=s;}
	
	 /**
     * @return the analyze
     */
	public String getAnalyze()
	{return this.analyze;}
	
	 /**
     * @return the sid
     */
	public int getSid()
	{return sid;}
	
	 /**
     * @return q (questions array)
     */
	public String[] getQ()
	{return q;}

	 /**
	 * @param i - index of the question to return 
     * @return q[i]- one question
     */
	public String getQ(int i)
	{
		if(i<0||i>5)	return null;
		else	return q[i];
	}

	 /**
     * handles the message received from the GUI and send it to the server.
     *       
     * checks if a survey is distributed in DB
     *       
     * @param sn - String array that holds the sid and the task. 
     */
	public static void getDistribute(String[] sn)
	{
		ObjHandle.sendToServer(sn);
		Survey_GUI.d= (Integer) Main.clie.getResult();
	}
	
	 /**
     * handles the message received from the GUI and send it to the server.
     *       
     * checks if a survey is distributed in DB
     *       
     * @param sn - String array that holds the sid and the task. 
     */	
	public int save_SurveyAnalyze()
	{
		ObjHandle.sendToServer(this);
		return (Integer) Main.clie.getResult();
	}
	
	/**
     * handles the message received from the GUI and send it to the server      
     * set in the Survey_GUI array of the survey sid
     */	
	public void get_SurveySid()	
	{		
		ObjHandle.sendToServer("Survey");
		Survey_GUI.s = (Integer[]) Main.clie.getResult();
	}
	
	/**
	 * handles the message received from the GUI and send it to the server
	 * set in the Survey_GUI array of the survey name
	 */
	public void get_DistributedSurveySid()	
	{		
		ObjHandle.sendToServer("Distributed Survey Sname");
		Survey_GUI.st = (Integer[]) Main.clie.getResult();
	}
	
	/**
	 * @param sn ="Display Survey" , survey sid
	 * @return Survey that has an array of questions
	 */
	public static Survey dispSurvey(String sn[]) 
	{
		ObjHandle.sendToServer(sn);
		return (Survey) Main.clie.getResult();
	}
	/**
	 * checks if there is an analyze of the survey
	 * @return 1 - if there is analyze to the survey, 0 - if no
	 */
	public int check_analyze() 
	{
		if(analyze.equals("-1"))	return 0;
		return 1;
	}
	/**
	 * distribute survey from database
	 * @param sn= "Get Distribute"(for the echo server), sid
	 */
	public static void distribute(String[] sn)
	{
		ObjHandle.sendToServer(sn);
		Survey_GUI.disributeFlag= (Integer) Main.clie.getResult();
	}
	/**
	 * for employee to read a distributed survey(set it as read)
	 * @param sn "readSurvey"(for the echo server), sid
	 */
	public static void readSurvey(String[] sn) 
	{
		ObjHandle.sendToServer(sn);
	}
	/**
	 * sets into MainScreen.read the number of unread surveys
	 */
	public static void checkRead()
	{
		ObjHandle.sendToServer("checkRead");
		MainScreen.read= (Integer) Main.clie.getResult();
	}
}